package com.yixun.lbx.controller.company;

import com.yixun.controller.BaseController;
import com.yixun.entity.DiQu;
import com.yixun.entity.YxUser;
import com.yixun.lbx.entity.YxProCompany;
import com.yixun.lbx.service.CompanyService;
import com.yixun.util.Const;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/*
 * 企业信息 控制器
 * */
@RestController
@RequestMapping("/ci")
public class CompanyController extends BaseController {
    private final CompanyService companyService;

    public CompanyController(CompanyService companyService) {
        this.companyService = companyService;
    }

    /**
     * @param map todo
     * @return java.util.HashMap<java.lang.String, java.lang.Object>
     * @apiNote getListByPage 企业（分页）列表
     */
    @RequestMapping("/getListByPage")
    public HashMap<String, Object> getListByPage(@RequestBody HashMap<String, String> map) {
        Integer companyCount = companyService.CompanyCount();
        List<YxProCompany> companies = companyService.getListByPage(map);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("companyCount", companyCount);
        hashMap.put("companies", companies);
        return hashMap;
    }

    /**
     * @param company todo
     * @return com.yixun.lbx.entity.YxProCompany
     * @apiNote getCompanyByIdOrNameOrCode 查找企业通过id || name || 统一社会信用代码
     */
    @PostMapping("/getCompanyByIdOrNameOrCode")
    public YxProCompany getCompanyByIdOrNameOrCode(YxProCompany company) {
        return companyService.getCompanyByIdOrNameOrCode(company);
    }

    /**
     * @param company todo
     * @return java.lang.Integer
     * @apiNote addCompany 添加企业
     */
    @PostMapping("/addCompany")
    public Integer addCompany(HttpServletRequest request, @RequestBody YxProCompany company) {
        YxUser user = this.getUser(request);
        company.setAddUser(user.getUser_id());
        return companyService.addCompany(company);
    }

    /**
     * @param company todo
     * @return java.lang.Integer
     * @apiNote updateCompany 更新企业
     */
    @PostMapping("/updateCompany")
    public Integer updateCompany(HttpServletRequest request, @RequestBody YxProCompany company) {
        if (this.getQyId() !=0){
            company.setQyId(this.getQyId());
        }
        YxUser user = this.getUser(request);
        company.setUpdateUser(user.getUser_id());
        return companyService.updateCompany(company);
    }

    /**
     * @param map     todo
     * @return java.lang.Integer
     * @apiNote deteleCompany 停用企业账号并停用企业信息
     */
    @RequestMapping("/deteleCompany")
    public Integer deteleCompany(HttpServletRequest request, @RequestBody HashMap<String, Object> map) {
        YxUser user = this.getUser(request);
        String updateUser = user.getUser_id();
        map.put("updateUser", updateUser);
        return companyService.deleteCompany(map);
    }


    /**
     * @param map todo
     * @return java.util.HashMap<java.lang.String, java.lang.Object>
     * @apiNote getIsDeleteListByPage 已停用企业（分页）列表
     */
    @RequestMapping("/getIsDeleteListByPage")
    public HashMap<String, Object> getIsDeleteListByPage(@RequestBody HashMap<String, String> map) {
        return companyService.getIsDeleteListByPage(map);
    }

    /**
     * @param map     todo
     * @return java.lang.Integer
     * @apiNote enableCompany 启用企业
     */
    @RequestMapping("/enable")
    public Integer enableCompany(HttpServletRequest request, @RequestBody HashMap<String, Object> map) {
        YxUser user = this.getUser(request);
        String updateUser = user.getUser_id();
        map.put("updateUser", updateUser);
        return companyService.enableCompany(map);
    }

    /**
     * @param request todo
     * @return com.yixun.entity.YxUser
     * @apiNote getUser 获取当前登录用户
     */
    public YxUser getUser(HttpServletRequest request) {
        return (YxUser) request.getSession().getAttribute(Const.SESSION_USER);
    }

    /**
     * @param map todo
     * @return java.util.HashMap<java.lang.String, java.lang.Object>
     * @apiNote getSearchListByPage 搜索
     */
    @RequestMapping("/getSearchListByPage")
    public HashMap<String, Object> getSearchListByPage(@RequestBody HashMap<String, String> map) {
        HashMap<String, Object> hashMap = new HashMap<>();
        List<YxProCompany> companies = companyService.getSearchListByPage(map);
        hashMap.put("companies", companies);
        hashMap.put("count", companies.size());
        return hashMap;
    }
    private Long getQyId() {
        YxProCompany yxProCompany = new YxProCompany();
        yxProCompany.setCode(this.getSessionUser().getUser_name());
        YxProCompany company = companyService.getCompanyByIdOrNameOrCode(yxProCompany);
        if (company != null) {
            return company.getQyId();
        } else {
            return 0L;
        }
    }
    @GetMapping("/getChildDiquListByDq3Name/{area}")
    public List<DiQu> getChildDiquListByDq3Name(@PathVariable String area){
       return companyService.getChildDiquListByDq3Name(area);
    }
}
